package ducct.footprint.db;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import ducct.footprint.entity.MyFootprint;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/*
 * Provide helper functions to interact with database
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	public final String TAG = "DatabaseHelper";
	public static boolean NEED_DROP_OLD_DB = false;
	
	private static final String DB_NAME = "footprint";
	private static final String TABLE_MY_FOOTPRINT = "my_footprint";
	
	private SQLiteDatabase db;
	private static DatabaseHelper instance = null;

	public static DatabaseHelper getInstance(Context context) {
		if (instance == null) {
			instance = new DatabaseHelper(context);
			
			if (NEED_DROP_OLD_DB) {
				instance.db.execSQL("drop table " + TABLE_MY_FOOTPRINT);
			}
			
			instance.createTableMyFootprint();
		}

		return instance;
	}

	private DatabaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		db = context.openOrCreateDatabase(DB_NAME, 0, null);
	}
	
	public void beginBatchOperation() {
		db.beginTransaction();
	}

	public void endBatchOperation() {
		db.setTransactionSuccessful();
		db.endTransaction();
	}

	public boolean isNotOpen() {
		return !db.isOpen();
	}

	@Override
	public void close() {
		synchronized (db) {
			if (db != null)
				db.close();
		}
		super.close();
	}

	@Override
	public void onCreate(SQLiteDatabase db) {

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

	}
	
	public boolean isExistTable(String tableName) {
		Cursor cursor = 
			db.rawQuery("SELECT * FROM SQLITE_MASTER WHERE NAME = '" + tableName + "'",
					null);
		if (cursor.getCount() == 0) {
			cursor.close();
			return false;
		}
		cursor.close();
			
		return true;
	}
	
	public List<MyFootprint> getAllMyFootprints() {
		Cursor cursor = db.rawQuery("select * from " + TABLE_MY_FOOTPRINT + " order by timestamp desc", null);
		
		List<MyFootprint> list = new LinkedList<MyFootprint>();
		if (cursor.moveToFirst()) {
			do {
				list.add(new MyFootprint(cursor));
			} while (cursor.moveToNext());
		}
		cursor.close();
		
		return list;
	}
	
	public void saveMyFootprint(MyFootprint myFootprint) {
		db.execSQL("insert into " + TABLE_MY_FOOTPRINT + " (locationName, lat, lng, imagePath, timestamp, note)" + 
				" values (?, ?, ?, ?, ?, ?)", 
				new String[] {
					myFootprint.locationName,
					myFootprint.lat + "",
					myFootprint.lng + "",
					myFootprint.imagePath,
					myFootprint.timestamp + "",
					myFootprint.note
		});
	}
	
	public void deleteMyFootprint() {
		
	}
	
	public void deleteAllMyFootprint() {
		
	}
	
	public void deleteMyFootprint(List<MyFootprint> list) {
		
	}
	
	private static final String QUERY_CREATE_TABLE = 
		"create table " + TABLE_MY_FOOTPRINT + " (locationName, lat, lng, imagePath, timestamp, note)";
	private void createTableMyFootprint() {
		if (!isExistTable(TABLE_MY_FOOTPRINT)) {
			db.execSQL(QUERY_CREATE_TABLE);
		}
	}
}
